#include <QCoreApplication>
#include <QSqlDatabase>
#include <QSqlQuery>
#include "test_qyorm.h"
#include <QFile>

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);


    Qy::Table::db = QSqlDatabase::addDatabase("QSQLITE");
    QSqlDatabase& db = Qy::Table::db;
    db.setDatabaseName("test.db");

    RegisterForeign(User);//外键需要注册一下

#if 01
//这里是创建表结构，和插入一些测试数据
    db.open();
    DF << QSqlQuery(db).exec("create table User(uid varchar(255) primary key, pwd varchar(255) unique not null, name varchar(255) not null);");
    DF << QSqlQuery(db).exec("create table Info(iid varchar(255) primary key, uid varchar(255), date varchar(255) not null, "
                             "foreign key(uid) references User(uid));");
    DF << QSqlQuery(db).exec("insert into User values('001', '001', '赵一');");
    DF << QSqlQuery(db).exec("insert into User values('002', '002', '钱二');");
    DF << QSqlQuery(db).exec("insert into User values('003', '003', '张三');");
    DF << QSqlQuery(db).exec("insert into User values('004', '004', '李四');");
    db.close();

#elif 0
//这里是全部查询
    for(User* user : QyOrmSelect(User())){//查询
        DF << user->uid.get() << user->pwd.get() << user->name.get();
    }

#elif 0
//这里是条件查询
    for(User* user : QyOrmSelect(User().setField("uid", QString("002")))){//查询
        DF << user->uid.get() << user->pwd.get() << user->name.get();
    }

#elif 0
//这里是查询一条数据后修改
    User* user = QyOrmSelectOne(User().setField("uid", QString("001")));
    if(user){
//        DF << user->uid.get() << user->pwd.get() << user->name.get();
        DF << user->name.set("赵一");//修改并打印结果
        DF << user->isInstance();
//        delete user;//可以delete，也可以不delete，留在hash表里用于QyOrmInstance搜索
    }

//这里是查询一条实例
    User* userInstance = QyOrmInstance(User, "001");
    if(userInstance){
        DF << userInstance->uid.get() << userInstance->pwd.get() << userInstance->name.get();
    }

#elif 0
//这里是插入一条数据
    User* user = new User;
    user->uid = "005";
    user->pwd = "005";
    user->name = "孙五";
    DF << user->insert();//插入

#elif 0
//这里是删除一个记录
    User* user = QyOrmSelectOne(User().setField("uid", QString("005")));
    if(user){
        DF << user->uid.get() << user->pwd.get() << user->name.get();
        DF << user->remove();//删除
    }

#elif 0
//这里是查询一条数据和插入一条有外键表的数据
    User* user = QyOrmSelectOne(User().setField("uid", QString("002")));
    if(user){
        DF << user->uid.get() << user->pwd.get() << user->name.get();
        Info* info = new Info;
        info->iid = "1";
        info->uid = user;
        info->date = "20210524";
        DF << info->insert();
    }

#elif 0
//这里是查询一条有外键表的数据并且保存json
    Info* info = QyOrmSelectOne(Info().setField("iid", QString("1")));
    if(info){
        User* user = info->uid.get<User>();
        DF << info->iid.get() << user->uid.get() << info->date.get();
        QFile file("test.json");
        file.open(QFile::WriteOnly);
        file.write(info->toJson().toUtf8());
        file.close();
    }

#elif 0
//这里是删除所有new出来没有删除的table对象实例
    User::destoryAllInstance();

#endif

    DF << "Success";
    return 0;
}
